//---------------------------------------------------------------------------------
// Copyright (c) 2011, Microsoft Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//---------------------------------------------------------------------------------

namespace WindowsPhoneChat
{
    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Input;
    using System.Windows.Navigation;
    using Microsoft.Phone.Controls;
    using Microsoft.Samples.ServiceBus.WindowsPhoneChat.Controllers;
    using Microsoft.Samples.ServiceBus.WindowsPhoneChat.ViewModels;

    public partial class ChatPage : PhoneApplicationPage
    {
        public ChatPage()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            var viewModel = this.DataContext as ChatPageViewModel;
            if (viewModel != null)
            {
                viewModel.MessageReceived += this.OnMessageReceived;
                viewModel.OnPageOpened();
            }

            // Show 'Navigate in' animation
            VisualStateManager.GoToState(this, "Visible", true);
        }

        protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)
        {
            // Show 'Navigate out' animation
            VisualStateManager.GoToState(this, "NotVisible", true);

            base.OnNavigatingFrom(e);
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            var viewModel = this.DataContext as ChatPageViewModel;
            if (viewModel != null)
            {
                viewModel.OnPageClosed();
                viewModel.MessageReceived -= this.OnMessageReceived;
            }

            base.OnNavigatedTo(e);
        }

        private void OnMessageChanged(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                // Force the ViewModel to be updated to the current value of the TextBox's text
                var bindingExpression = ((TextBox)sender).GetBindingExpression(TextBox.TextProperty);
                bindingExpression.UpdateSource();

                var messageText = ((ChatPageViewModel)this.DataContext).CurrentMessage.Value;

                this.SendMessage();
            }
        }

        private void OnSendClicked(object sender, RoutedEventArgs e)
        {
            this.SendMessage();
        }

        private void SendMessage()
        {
            var viewModel = this.DataContext as ChatPageViewModel;
            if (viewModel != null)
            {
                viewModel.OnSendMessage();
            }
        }

        private void OnMessageReceived(object sender, MessageReceivedEventArgs e)
        {
            this.messagesTextBox.Text += e.Message.UserName + ": " + e.Message.Value + Environment.NewLine;
            this.messagesScroller.ScrollToVerticalOffset(this.messagesScroller.ScrollableHeight);
        }
    }
}
